﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using TravelLink.DAL;

namespace TravelLink.WebApp.UserControl.Member {
    public partial class UC_Member_ViewJob_CN : ControlBase {

        public Guid UserCode {
            get {
                Guid userCode = Guid.Empty;
                if (Guid.TryParse(WebHelper.GetQueryStringStr("uid"), out userCode)) {
                    return userCode;
                }
                else {

                    return this.CurrentUser.DBUser.UserCode;
                }
            }
        }



        public int ResumeId {
            get {

                if (UserCode != Guid.Empty) {
                    if (ResumeModel == null) {
                        ResumeModel = this.DB.TL_Resume.FirstOrDefault(u => u.Language == 1 && u.UserCode == UserCode);
                        ResumeUserModel = this.DB.TL_User.FirstOrDefault(u => u.UserCode == UserCode);
                    }
                    return ResumeModel.Idx;
                }
                return -1;
            }
        }


        public TL_Resume ResumeModel { get; private set; }

        public TL_User ResumeUserModel { get; private set; }


        public string BirthCity = string.Empty;
        public string PlaceCity = string.Empty;


        private bool CheckIsCreateResume() {
            bool flag = true;

            TL_Resume resume = null;
            if ((resume = this.DB.TL_Resume.Where(u => u.Language == 1 && u.UserCode == UserCode).FirstOrDefault()) == null) {
                flag = false;
                resume = new TL_Resume();
                resume.RowTime = DateTime.Now;
                resume.UserCode = UserCode;
                resume.Language = 1;

                DB.TL_Resume.InsertOnSubmit(resume);
                DB.SubmitChanges();

                Response.Redirect(this.Request.RawUrl);
                return flag;
            }

            return flag;

        }

        protected void Page_Load(object sender, EventArgs e) {
            if (!CheckIsCreateResume()) return;
            int resumeId = ResumeId;

            var rptCareeExpDataSourceList = this.DB.TL_User_CareerExperience.Where(u => u.ResumeId == resumeId).ToList();
            if (rptCareeExpDataSourceList.Count > 0) {
                this.rptCareeExpDataSource.DataSource = rptCareeExpDataSourceList;
                this.rptCareeExpDataSource.DataBind();
            }

            var rptEducationDataSourceList = this.DB.TL_User_Education.Where(u => u.ResumeId == resumeId).ToList();
            if (rptEducationDataSourceList.Count > 0) {
                this.rptEducationDataSource.DataSource = rptEducationDataSourceList;
                this.rptEducationDataSource.DataBind();
            }

            var rptQualificationDataSourceList = this.DB.TL_User_Qualification.Where(u => u.ResumeId == resumeId).ToList();
            if (rptQualificationDataSourceList.Count > 0) {
                this.rptQualificationDataSource.DataSource = rptQualificationDataSourceList;
                this.rptQualificationDataSource.DataBind();

            }

            var rptSkillDataSourceList = this.DB.TL_User_Skills.Where(u => u.UserCode == ResumeUserModel.UserCode).ToList();
            if (rptSkillDataSourceList.Count > 0) {
                this.rptSkillDataSource.DataSource = rptSkillDataSourceList;
                this.rptSkillDataSource.DataBind();
            }

            var rptAwardsDataSourceList = this.DB.TL_User_Awards.Where(u => u.ResumeId == resumeId).ToList();
            if (rptAwardsDataSourceList.Count > 0) {
                this.rptAwardsDataSource.DataSource = rptAwardsDataSourceList;
                this.rptAwardsDataSource.DataBind();
            }

            var rptMembershipDataSourceList = this.DB.TL_User_Membership.Where(u => u.ResumeId == resumeId).ToList();
            if (rptMembershipDataSourceList.Count > 0) {
                this.rptMembershipDataSource.DataSource = rptMembershipDataSourceList;
                this.rptMembershipDataSource.DataBind();
            }

            if (this.ResumeModel.PlaceCity.HasValue) {
                PlaceCity = DB.TL_City.Where(u => u.Idx == (this.ResumeModel.PlaceCity ?? 0)).Select(u => u.CName).FirstOrDefault();
            }
            if (this.ResumeModel.BirthCity.HasValue) {
                BirthCity = DB.TL_City.Where(u => u.Idx == (this.ResumeModel.BirthCity ?? 0)).Select(u => u.CName).FirstOrDefault();
            }


        }



        protected string GetName() {

            if (ResumeUserModel != null) {
                string name = string.Empty;
                if (!string.IsNullOrEmpty(ResumeUserModel.CFirstName)) {
                    name = ResumeUserModel.CFirstName;
                }
                if (!string.IsNullOrEmpty(ResumeUserModel.CLastName)) {

                    if (name.Length > 1) {
                        name += (" " + ResumeUserModel.CLastName);
                    }
                    else {
                        name = ResumeUserModel.CLastName;
                    }
                }
                return name;
            }

            return string.Empty;
        }



        protected string GetENName() {

            if (ResumeUserModel != null) {
                string name = string.Empty;
                if (!string.IsNullOrEmpty(ResumeUserModel.EFirstName)) {
                    name = ResumeUserModel.EFirstName;
                }
                if (!string.IsNullOrEmpty(ResumeUserModel.ELastName)) {

                    if (name.Length > 1) {
                        name += (" " + ResumeUserModel.ELastName);
                    }
                    else {
                        name = ResumeUserModel.ELastName;
                    }
                }
                return name;
            }

            return string.Empty;
        }


        protected string GetCountry() {

            if (this.ResumeUserModel.TL_Country != null) {
                return this.ResumeUserModel.TL_Country.CCountry;
            }
            return string.Empty;

        }


        protected string GetEducationType(object dataItem) {
            TL_User_Education education = (dataItem as TL_User_Education);
            if (education != null) {
                return education.TL_Education != null ? education.TL_Education.Name : string.Empty;
            }
            return string.Empty;
        }


    }


    
}